package com.clearance.mine_clearance.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clearance.mine_clearance.entry.Capital;
import com.clearance.mine_clearance.entry.Result;
import com.clearance.mine_clearance.entry.vo.CapitalVo;
import com.clearance.mine_clearance.mapper.CapitalMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin("*")
@RestController
@RequestMapping("/mine/capital")
public class CapitalController {


    @Autowired
    private CapitalMapper capitalMapper;

    @PostMapping("/selectPage")
    public Result selectPage(@RequestBody CapitalVo capitalVo){
        Page<Capital> capitalPage = new Page<>(capitalVo.getCurrent(),capitalVo.getSize());
        LambdaQueryWrapper<Capital> capitalLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(capitalVo.getType()!=null){
            capitalLambdaQueryWrapper.eq(Capital::getType,capitalVo.getType());
        }
        if(StringUtils.isNotEmpty(capitalVo.getFirstName())){
            capitalLambdaQueryWrapper.eq(Capital::getFirstName,capitalVo.getFirstName());
        }
        if(StringUtils.isNotEmpty(capitalVo.getUserName())){
            capitalLambdaQueryWrapper.eq(Capital::getUserName,capitalVo.getUserName());
        }
        if(capitalVo.getStartTime()!=null&&capitalVo.getEndTime()!=null){
            capitalLambdaQueryWrapper.between(Capital::getTime,capitalVo.getStartTime(),capitalVo.getEndTime());
        }
        capitalLambdaQueryWrapper.orderByDesc(Capital::getTime);
        Page<Capital> selectPage = capitalMapper.selectPage(capitalPage, capitalLambdaQueryWrapper);
        return new Result().success(selectPage);
    }
}


























